using System;
using Gtk;

namespace GtkTest {
public partial class MainWindow : Gtk.Window
{
/*
	protected Gtk.ComboBox testCombo;
	protected Gtk.Notebook mainNotebook;
	protected Gtk.TreeView mainTv;
*/
	TreeIter actorsIter;
	TreeIter directorsIter;
/*	protected Gtk.Image img;*/
	
	public MainWindow() : base(null)
	{
		Build();
		
		TreViewTest();
	}
	
	private void TreViewTest()
	{
		mainNotebook.CurrentPage = 1;
		TreeStore store = new TreeStore(typeof (int), typeof (string));
		actorsIter = store.AppendValues (-1, "Actori");
		directorsIter = store.AppendValues (-2, "Regizori");
		store.AppendValues (actorsIter, 1, "Actor 1");
		store.AppendValues (actorsIter, 2, "Actor 2");
		store.AppendValues (directorsIter, 3, "Regizor 1");
		store.AppendValues (directorsIter, 4, "Regizor 2");
		
		mainTv.AppendColumn ("Person", new CellRendererText (), "text", 1); 
		mainTv.Model = store;
	}
	
	private void ComboBoxTest()
	{
		mainNotebook.CurrentPage = 2;
		// combo box test
		ListStore comboStore = new ListStore (typeof(int), typeof(string));
		comboStore.AppendValues (1, "India");
		TreeIter itIter = comboStore.AppendValues (2, "Italia");
		comboStore.AppendValues (3, "Grecia");
		comboStore.AppendValues (4, "Spania");
		
		CellRendererText rnd = new CellRendererText ();
		testCombo.PackStart (rnd, true);
		testCombo.SetAttributes (rnd, "text", 1);
		testCombo.Model = comboStore;
		testCombo.SetActiveIter (itIter);
		
		TreeIter iter;
		if (comboStore.GetIterFirst (out iter)) {
			do {
				string s = (string)comboStore.GetValue (iter, 1);

				if (s == "Spania") {
					testCombo.SetActiveIter (iter);
					break;
				}
			} while (comboStore.IterNext (ref iter));
		}
	}

	protected virtual void OnDeleteEvent(object o, Gtk.DeleteEventArgs args)
	{
		Application.Quit ();
		args.RetVal = true;
	}

	protected virtual void OnQuitActivated(object sender, System.EventArgs e)
	{
		Application.Quit();
	}

	protected virtual void OnDeleteButtonClicked(object sender, System.EventArgs e)
	{
		TreeStore store = (TreeStore) mainTv.Model;
		
		TreePath path;
		TreeViewColumn col;
		TreeIter iter;
		
		// determinam iter-ul liniei selectate
		mainTv.GetCursor (out path, out col);
		store.GetIter (out iter, path);
		
		Console.WriteLine ((string) store.GetValue (iter, 1));
		store.Remove (ref iter);
	
		// selectam alta linie
		TreePath newPath = store.GetPath (iter);
		mainTv.SetCursor (newPath, col, false);
	}

	protected virtual void OnImgButtonClicked(object sender, System.EventArgs e)
	{
		FileChooserDialog fc = new FileChooserDialog("Choose the file to open",
			this,
			FileChooserAction.Open,
			"Cancel", ResponseType.Cancel,
			"Open", ResponseType.Accept);

          if (fc.Run () == (int) ResponseType.Accept) {
               System.IO.FileStream file = System.IO.File.OpenRead (fc.Filename);
               file.Close();
          }
          fc.Destroy();
	}
}
}